import axios from 'axios'

export default {
  namespaced: true,
  
  state: {
    healthData: [],
    loading: false,
    error: null
  },
  
  getters: {
    allHealthData: state => state.healthData,
    loading: state => state.loading,
    error: state => state.error,
    
    // 按类型获取健康数据
    healthDataByType: state => type => {
      return state.healthData.filter(data => data.data_type === type);
    },
    
    // 获取最新的数据类型值
    latestDataByType: state => type => {
      const filtered = state.healthData.filter(data => data.data_type === type);
      return filtered.length > 0 ? filtered[0] : null;
    }
  },
  
  mutations: {
    SET_LOADING(state, status) {
      state.loading = status;
    },
    SET_ERROR(state, error) {
      state.error = error;
    },
    SET_HEALTH_DATA(state, data) {
      state.healthData = data;
    },
    ADD_HEALTH_DATA(state, data) {
      state.healthData.unshift(data);
    },
    UPDATE_HEALTH_DATA(state, updatedData) {
      const index = state.healthData.findIndex(item => item.id === updatedData.id);
      if (index !== -1) {
        state.healthData.splice(index, 1, updatedData);
      }
    },
    DELETE_HEALTH_DATA(state, id) {
      state.healthData = state.healthData.filter(data => data.id !== id);
    }
  },
  
  actions: {
    // 获取所有健康数据
    async fetchHealthData({ commit }) {
      try {
        commit('SET_LOADING', true);
        commit('SET_ERROR', null);
        
        const response = await axios.get('/health/data');
        commit('SET_HEALTH_DATA', response.data);
        
        commit('SET_LOADING', false);
        return response.data;
      } catch (error) {
        commit('SET_LOADING', false);
        commit('SET_ERROR', error.response?.data?.message || '获取健康数据失败');
        throw error;
      }
    },
    
    // 添加健康数据
    async addHealthData({ commit }, healthData) {
      try {
        commit('SET_LOADING', true);
        commit('SET_ERROR', null);
        
        const response = await axios.post('/health/data', healthData);
        commit('ADD_HEALTH_DATA', response.data);
        
        commit('SET_LOADING', false);
        return response.data;
      } catch (error) {
        commit('SET_LOADING', false);
        commit('SET_ERROR', error.response?.data?.message || '添加健康数据失败');
        throw error;
      }
    },
    
    // 更新健康数据
    async updateHealthData({ commit }, { id, healthData }) {
      try {
        commit('SET_LOADING', true);
        commit('SET_ERROR', null);
        
        const response = await axios.put(`/health/data/${id}`, healthData);
        commit('UPDATE_HEALTH_DATA', response.data);
        
        commit('SET_LOADING', false);
        return response.data;
      } catch (error) {
        commit('SET_LOADING', false);
        commit('SET_ERROR', error.response?.data?.message || '更新健康数据失败');
        throw error;
      }
    },
    
    // 删除健康数据
    async deleteHealthData({ commit }, id) {
      try {
        commit('SET_LOADING', true);
        commit('SET_ERROR', null);
        
        await axios.delete(`/health/data/${id}`);
        commit('DELETE_HEALTH_DATA', id);
        
        commit('SET_LOADING', false);
        return true;
      } catch (error) {
        commit('SET_LOADING', false);
        commit('SET_ERROR', error.response?.data?.message || '删除健康数据失败');
        throw error;
      }
    },
    
    // 获取特定类型的健康数据
    async fetchHealthDataByType({ commit }, type) {
      try {
        commit('SET_LOADING', true);
        commit('SET_ERROR', null);
        
        const response = await axios.get(`/health/data/type/${type}`);
        // 这里我们不更新整个健康数据列表，而是返回查询结果
        
        commit('SET_LOADING', false);
        return response.data;
      } catch (error) {
        commit('SET_LOADING', false);
        commit('SET_ERROR', error.response?.data?.message || '获取健康数据失败');
        throw error;
      }
    }
  }
} 